<?php
// +----------------------------------------------------------------------
// | 海豚PHP框架 [ DolphinPHP ]
// +----------------------------------------------------------------------
// | 版权所有 2016~2017 河源市卓锐科技有限公司 [ http://www.zrthink.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://dolphinphp.com
// +----------------------------------------------------------------------
// | 开源协议 ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------

namespace app\index\controller;

use app\common\controller\Common;
use app\index\model\Type as TypeModel;
use app\index\model\Search as SearchModel;
use think\Db;
use think\Config;
/**
 * 前台公共控制器
 * @package app\index\controller
 */
class Home extends Common
{
    protected $mid = 0;//当前账号id
    protected $pmid = 0;//父级账号id
    protected $member = [];//当前账户信息
    /**
     * 初始化方法
     * @author 蔡伟明 <314013107@qq.com>
     */
    protected function _initialize()
    {
        // 系统开关
        if (!config('web_site_status')) {
            $this->error('站点已经关闭，请稍后访问~');
        }
        $this->mid=session("uid");
        $this->autoLogin();
        $this->hotSearch();
        $this->captchaset();
    }

    /**
    *获取文件路径
    */
    public function getPath($id = 0){
       if($id == 0){
        return false;
       }
       $all_data = AttachmentModel::getAll();
        $path = $all_data[$id]['path'];
        return $path;
    }


    //左侧导航
    public function left_category(){
        //顶级分类
        $all_type = [];
        $parent_category = [];
        $all_type = TypeModel::getAll();
        foreach ($all_type as $key => $value) {
            if($value['status'] == 1 && $value['show'] == 1 && $value['parentid'] == 0){
                $parent_category[] = $value;
            }
        }
        $son_data = [];
        foreach ($all_type as $key => $value) {
            $son_data = [];
            foreach ($parent_category as $k => $val) {
                if($value['status'] == 1 && $value['show'] == 1 && $value['parentid'] == $val['typeid']){
                    $son_data[] = $value;
                }
                $parent_category[$k]['son_category'] = array_chunk($son_data, 3);
            }
        }
        
        $this->assign('parent_category', $parent_category);
    }

    //热门搜索
    public function hotSearch(){
        $hot_search =  SearchModel::topList(5);
        $this->assign('hot_search',$hot_search);
    }

    public function autoLogin(){
        $field=['id,mobile,password,company,id,l_mobile,parentid,member_level'];
        if(empty($this->mid)){
            $login_auto=cookie('auto');
            if(!empty($login_auto)){
                $auto = explode('|', authcode($login_auto));
                $ip = get_client_ip();
                if ($auto[2] == $ip) {
                    $member = Db::name('member')->where('id',$auto[1])->field($field)->find();
                    if($member){
                        session('uid',$member['id']);
                        session('mobile', $member['mobile']);
                        session('puid',$member['parentid']);
                        session('company',$member['company']);
                        $this->member = $member;
                        $this->mid = $member['id'];
                        $this->pmid = $member['parentid'];
                    }
                }
            }
        }else{
            $member = Db::name('member')->where('id',$this->mid)->field($field)->find();
            if($member){
                session('uid',$member['id']);
                session('mobile', $member['mobile']);
                session('puid',$member['parentid']);
                session('company',$member['company']);
                $this->member = $member;
                $this->mid = $member['id'];
                $this->pmid = $member['parentid'];
            }
        }
    }
    public function captchaset(){
        $new_verify_config=cache('verifyconfig');
        //if(empty($new_verify_config)){
            $verifydata=Db::name("admin_config")->where(['group'=>'verify','status'=>1])->order(array('id'=>'desc'))->select();
            foreach ($verifydata as $r) {
                if($r['value']==1){
                    $r['value']=true;
                }else if($r['value']==0){
                    $r['value']=false;
                }
                $verify_config[$r['name']] = $r['value'];
            }
            $current_verify_config=Config::get("captcha"); 
            $new_verify_config=array_merge($verify_config,$current_verify_config);
            cache('verifyconfig',$new_verify_config);
        //}
        Config::set("captcha",$new_verify_config);
    }
    
}
